<template>
	<view>
		<u-navbar title="我的消息" title-color="#fff" :background="background" back-icon-color="#ffffff"
			:back-text-style="{color: '#fff'}">
			<view class="slot-wrap">
				<u-icon name="list-dot" color="#ffffff" size="36" @click="changeModel"></u-icon>
			</view>
		</u-navbar>
		<u-search class="searchBar" placeholder="搜索消息内容" v-model="keyword" :clearabled="true" @search="searchFunc"
			@custom="searchFunc" :show-action="true"></u-search>
		<view class="dualSelectArea" v-for="(item, index) in noticeArr" :key="index">
			<view class="dualSelectItem" @click="selectClick(item, index)">
				<u-icon class="iconStyle" name="checkmark-circle" color="#666666" size="35"
					v-show="editModel && !item.select"></u-icon>
				<u-icon class="iconStyle" name="checkmark-circle-fill" color="#2979ff" size="35"
					v-show="editModel && item.select"></u-icon>
				<myNotice :noticeItem="item"></myNotice>
			</view>
		</view>
		<view class="buttonArea" v-show="editModel">
			<u-button type="warning" shape="circle" @click="selectAll">全选</u-button>
			<u-button type="success" shape="circle" @click="selectRead">已读
				<text v-show="selectAmount>0">({{selectAmount}})</text>
			</u-button>
			<u-button type="error" shape="circle" @click="selectDelete">删除
				<text v-show="selectAmount>0">({{selectAmount}})</text>
			</u-button>
		</view>
		<u-modal v-model="ModalShow" content="是否确认删除选中项目？" :show-cancel-button="true" :mask-close-able="true"
			@confirm="confirmDelete"></u-modal>
		<u-loadmore :status="status" />

	</view>
</template>

<script>
	import dualSelect from '@/components/dual-select/dual-select'
	import myNotice from '@/components/myNotice/myNotice'
	export default {
		components: {
			dualSelect,
			myNotice
		},
		data() {
			return {
				noticeArr: [],
				status: 'loadmore',
				page: 1,
				keyword: '',
				background: {
					'background-image': 'linear-gradient(45deg, #007fd1, #013cd2)'
				},
				dataList: [{
						id: "1",
						select: false
					},
					{
						id: "2",
						select: false
					},
					{
						id: "3",
						select: false
					},
				],
				ModalShow: false,
				editModel: false,
				selectAmount: 0, //记录被选中的项目的数量
				selectArr: [], //记录被选中的项目的id
				isSelectAll: false
			}
		},

		methods: {
			searchFunc(keyword) {
				console.log(keyword);
				var that = this
				this.$api.myNotice(1, keyword)
					.then(res => {
						uni.showToast({
							title: '搜索成功'
						})
						console.log(res);
						// console.log('考试列表:',res.values.records);
						that.noteList = res.values.records
					})
			},
			changeModel() {
				// this.$refs.dualSelect.changeModel()
				this.editModel = !this.editModel
			},
			selectClick(item, index) {
				// if (this.options.itemCanSelect && this.editModel) {
				if (this.editModel) {
					// select 变量用于保存是否被选中
					this.noticeArr[index].select = !this.noticeArr[index].select
				}
				if (item.select) {
					this.selectAmount++
					this.selectArr.push(item.id)
				} else {
					this.selectAmount--
					this.selectArr.pop(item.id)
				}
			},
			selectDelete() {
				this.ModalShow = true;
			},
			confirmDelete() {
				let selectStr = this.selectArr.join(',')
				console.log(selectStr);
				this.$api.deleteNotice(selectStr)
					.then(res => {
						console.log('delBatch:', res)
						// 强制刷新
						uni.navigateBack({
							delta: 0,
						});
						// uni.redirectTo({
						// 	url: `pages/mine/myNotice`,
						// });
					})
			},
			selectRead() {
				let selectStr = this.selectArr.join(',')
				console.log(selectStr);
				this.$api.readNotice(selectStr)
					.then(res => {
						console.log('delBatch:', res)
						// 强制刷新
						uni.navigateBack({
							delta: 0,
						});
					})
			},
			selectAll() {
				// 全选与取消功能
				var that = this
				if (that.isSelectAll) {
					// 取消选择
					for (let item of this.noticeArr) {
						item.select = false
						that.selectArr.pop(item.id)
					}
					that.isSelectAll = false
				} else {
					// 进行全选
					for (let item of this.noticeArr) {
						item.select = true
						that.selectArr.push(item.id)
					}
					that.isSelectAll = true
				}
			},
			// itemClick(e) {
			// 	console.log('列表点击了：', e)
			// },
			// finish(e) {
			// 	console.log('所有的选择：', e)
			// },
			// change(e) {
			// 	console.log('发生改变了：', e)
			// }
		},
		onLoad() {
			this.$api.myNotice(1, '')
				.then(res => {
					console.log(res);
					this.noticeArr = res.values.records
				})
		},
		onReachBottom() {
			if (this.status === 'loadmore') {
				this.page += 1
				this.status = 'loading'
				this.$api.myNotice(this.page, this.keyword)
					.then(res => {
						if (res.values.records.length === 0) {
							this.status = 'nomore'
						} else {
							this.noticeArr = this.noticeArr.concat(res.values.records)
							this.status = 'loadmore'
						}
					})
			}
		}
	}
</script>

<style>
	.searchBar {
		padding: 20upx;
	}

	.slot-wrap {
		flex: 1; // 让slot内容占满整个导航栏的宽度
		display: flex;
		justify-content: flex-end;
		/* align-items: center; */
		margin-right: 40rpx;
	}

	.dualSelectItem {
		display: flex;
		align-items: center;
		/*  flex-wrap: wrap; */
	}

	.iconStyle {
		padding: 0 0 0 15rpx;
		// transition: 0.5s;
	}

	.buttonArea {
		position: fixed;
		// background-color: #FFFFFF;
		background-color: #F1F1F1;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100upx;
		z-index: 99;
		box-shadow: 0 0 5px 1px rgba($color: #000000, $alpha: 0.1);
		display: flex;
		// justify-content: ;
		align-items: center;
	}
</style>
